// 角色定义
export const ROLES = {
    PLATFORM_ADMIN: 'admin',    // 平台管理员
    COMPANY_ADMIN: 'companyAdmin',      // 公司管理员
    NORMAL_USER: 'normalUser'          // 普通用户
}

// 权限定义
export const PERMISSIONS = {
    // 监控中心权限
    DASHBOARD: {
        name: '监控中心',
        VIEW_SIMPLE_DASHBOARD: {
            code: 'view_simple_dashboard',
            name: '查看首页',
            allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
            apis: [
                { method: 'GET', path: '/meters' },
                { method: 'GET', path: '/company/' },
                { method: 'GET', path: '/area' },
                { method: 'GET', path: '/meters' },
            ]
        },
    },

    // 用能数据权限
    ENERGY_DATA: {
        name: '能源数据',
        // 用水数据
        WATER_DATA: {
            name: '水资源数据',
            VIEW_CHART: {
                code: 'view_water_chart',
                name: '查看用水图表',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/report/meter' },
                    { method: 'GET', path: '/report/company' }
                ]
            },
            // 查看用水单耗定额
            VIEW_WATER_CONSUMPTION_QUOTA: {
                code: 'view_water_consumption_quota',
                name: '查看用水单耗定额',
                allowedRoles: [ROLES.PLATFORM_ADMIN],
                apis: []
            },
            VIEW_DAILY_REPORT: {
                code: 'view_water_daily_report',
                name: '查看用水按时段按日报表',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/detail' },
                    { method: 'GET', path: '/report/meter' },
                ]
            },
            // 查看日抄表记录
            VIEW_DAILY_STATISTICS_REPORT: {
                code: 'view_water_daily_statistics_report',
                name: '日抄表记录',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/detail' },
                    { method: 'GET', path: '/report/meter' },
                ]
            },
            // 查看日统计报表
            VIEW_DAILY_STATISTICS_REPORT_DETAIL: {
                code: 'view_water_daily_statistics_report_detail',
                name: '日统计报表',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/detail' },
                ]
            },
            // 查看月统计报表
            VIEW_MONTH_STATISTICS_REPORT: {
                code: 'view_water_month_statistics_report',
                name: '月统计报表',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/detail' },
                    { method: 'GET', path: '/report/meter' },
                ]
            },
            VIEW_COST: {
                code: 'view_water_cost',
                name: '查看用水费用',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/price' },
                ]
            },
            // 设置水费价格
            SET_WATER_PRICE: {
                code: 'set_water_price',
                name: '设置水费价格',
                allowedRoles: [ROLES.PLATFORM_ADMIN],
                apis: [
                    { method: 'GET', path: '/price_rule' },
                    { method: 'POST', path: '/price_rule' },
                    { method: 'PATCH', path: '/price_rule' },
                    { method: 'DELETE', path: '/price_rule' },
                ]
            },
            // 允许填写实缴水费
            ALLOW_FILL_ACTUAL_WATER_CHARGE: {
                code: 'allow_input_water_charge',
                name: '填写实缴水费',
                allowedRoles: [ROLES.PLATFORM_ADMIN],
                apis: []
            },
            // 查看水平衡
            VIEW_WATER_BALANCE: {
                code: 'view_water_balance',
                name: '查看水平衡',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: []
            },
        },

        // 用电数据
        ELECTRICITY_DATA: {
            name: '电能数据',
            VIEW_CHART: {
                code: 'view_electricity_chart',
                name: '查看用电图表',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/report/meter' },
                    { method: 'GET', path: '/report/company' }
                ]
            },
            VIEW_DAILY_REPORT: {
                code: 'view_electricity_daily_report',
                name: '查看用电按时段按日报表',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/detail' },
                    { method: 'GET', path: '/report/meter' },

                ]
            },
            // 查看日抄表记录
            VIEW_DAILY_STATISTICS_REPORT: {
                code: 'view_electricity_daily_statistics_report',
                name: '日抄表记录',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/detail' },
                    { method: 'GET', path: '/report/meter' },
                ]
            },
            // 查看日统计报表
            VIEW_DAILY_STATISTICS_REPORT_DETAIL: {
                code: 'view_electricity_daily_statistics_report_detail',
                name: '日统计报表',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/detail' },
                ]
            },
            // 查看月统计报表
            VIEW_MONTH_STATISTICS_REPORT: {
                code: 'view_electricity_month_statistics_report',
                name: '月统计报表',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/detail' },
                    { method: 'GET', path: '/report/meter' },
                ]
            },
            VIEW_COST: {
                code: 'view_electricity_cost',
                name: '查看电费管理',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: []
            },
            // 设置电费价格
            SET_ELECTRICITY_PRICE: {
                code: 'set_electricity_price',
                name: '设置电费价格',
                allowedRoles: [ROLES.PLATFORM_ADMIN],
                apis: []
            },
            // 允许填写实缴电费
            ALLOW_FILL_ACTUAL_ELECTRICITY_CHARGE: {
                code: 'allow_input_electricity_charge',
                name: '填写实缴电费',
                allowedRoles: [ROLES.PLATFORM_ADMIN],
                apis: []
            },
        },
        //用气数据
        GAS_DATA: {
            name: '燃气数据',
            VIEW_CHART: {
                code: 'view_gas_chart',
                name: '查看用气图表',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/report/meter' },
                    { method: 'GET', path: '/report/company' }
                ]
            },
            VIEW_DAILY_REPORT: {
                code: 'view_gas_daily_report',
                name: '查看用气按时段按日报表',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/detail' },
                    { method: 'GET', path: '/report/meter' },
                ]
            },
            // 查看日抄表记录
            VIEW_DAILY_STATISTICS_REPORT: {
                code: 'view_gas_daily_statistics_report',
                name: '日抄表记录',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/detail' },
                    { method: 'GET', path: '/report/meter' },
                ]
            },
            // 查看日统计报表
            VIEW_DAILY_STATISTICS_REPORT_DETAIL: {
                code: 'view_gas_daily_statistics_report_detail',
                name: '日统计报表',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/detail' },
                ]
            },
            // 查看月统计报表
            VIEW_MONTH_STATISTICS_REPORT: {
                code: 'view_gas_month_statistics_report',
                name: '月统计报表',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/meters' },
                    { method: 'GET', path: '/detail' },
                    { method: 'GET', path: '/report/meter' },
                ]
            },
            VIEW_COST: {
                code: 'view_gas_cost',
                name: '查看用气费用',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/price_rule' },
                ]
            },
        },
        Green_CARBON: {
            name: '能碳',
            VIEW_CARBON_DATA: {
                code: 'view_carbon_data',
                name: '查看能源数据',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
                apis: [
                    { method: 'GET', path: '/algorithm/basic_carbon' }
                ]
            }
        },
        //用能计划
        ENERGY_PLAN: {
            name: '用能计划',
            VIEW_PLAN: {
                code: 'view_plan',
                name: '查看用能计划',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN],
                apis: [
                    { method: 'GET', path: '/plan_v2' },
                ]
            },
            MANAGE_WATER_PLAN: {
                code: 'manage_plan',
                name: '管理用能计划',
                allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN],
                apis: [
                    { method: 'POST', path: '/plan_v2' },
                    { method: 'PATCH', path: '/plan_v2' },
                    { method: 'DELETE', path: '/plan_v2' }
                ]
            },
        },
    },

    // 设备中心权限
    DEVICE_CENTER: {
        name: '设备中心',
        VIEW_CONSOLE: {
            code: 'view_device_console',
            name: '查看设备控制台',
            allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
            apis: []
        },
        CONTROL_DEVICE: {
            code: 'control_device',
            name: '控制设备',
            allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
            apis: []
        },
        // 查看设备监控
        VIEW_DEVICE_MONITOR: {
            code: 'view_device_monitor',
            name: '查看设备监控',
            allowedRoles: [ROLES.PLATFORM_ADMIN],
            apis: [
                { method: 'GET', path: '/pipeline' },
                { method: 'GET', path: '/meters' },
            ]
        },
        VIEW_DEVICE_INFO: {
            code: 'view_device_info',
            name: '查看设备信息',
            allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
            apis: [
                { method: 'GET', path: '/meters' },
            ]
        },
        // 查看设备架构
        VIEW_DEVICE_ARCHITECTURE: {
            code: 'view_device_architecture',
            name: '查看设备架构',
            allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
            apis: [
                { method: 'GET', path: '/meters' },
            ]
        },
        // 管理设备
        MANAGE_DEVICE: {
            code: 'manage_device',
            name: '管理设备',
            allowedRoles: [ROLES.PLATFORM_ADMIN],
            apis: [
                { method: 'POST', path: '/meters' },
                { method: 'PATCH', path: '/meters' },
                { method: 'DELETE', path: '/meters' }
            ]
        }
    },

    // 异常预警中心权限
    WARNING_CENTER: {
        name: '异常预警中心',
        VIEW_EXCEPTIONS: {
            code: 'view_exceptions',
            name: '查看异常',
            allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
            apis: [
                { method: 'GET', path: '/event' }
            ]
        },
        HANDLE_EXCEPTIONS: {
            code: 'handle_exceptions',
            name: '处理异常',
            allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
            apis: [
                { method: 'PATCH', path: '/event' }
            ]
        },
        // 设置异常
        SET_EXCEPTION: {
            code: 'set_exception',
            name: '设置单位可推送的异常',
            allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN],
            apis: [
                { method: 'GET', path: '/event/setting' },
                { method: 'POST', path: '/event/setting' },
                { method: 'PATCH', path: '/event/setting' },
                { method: 'DELETE', path: '/event/setting' }
            ]
        },
        // 可以把异常信息推送到自己的邮箱
        PUSH_EXCEPTION_EMAIL: {
            code: 'push_exception_email',
            name: '推送异常到邮箱',
            allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
            apis: [
                { method: 'GET', path: '/notify' },
                { method: 'POST', path: '/notify' },
                { method: 'PATCH', path: '/notify' },
                { method: 'DELETE', path: '/notify' }
            ]
        },
        // 查看智能漏损分析
        VIEW_LEAKAGE: {
            code: 'view_leakage',
            name: '查看智能漏损分析',
            allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN, ROLES.NORMAL_USER],
            apis: [
                { method: 'GET', path: '/algorithm/water_static' },
                { method: 'GET', path: '/detail' },
            ]
        }
    },

    // 系统管理权限
    SYSTEM_MANAGEMENT: {
        name: '系统管理',
        MANAGE_COMPANY: {
            code: 'manage_company',
            name: '管理公司',
            allowedRoles: [ROLES.PLATFORM_ADMIN],
            apis: [
                { method: 'GET', path: '/company/' },
                { method: 'POST', path: '/company/' },
                { method: 'PATCH', path: '/company/' },
                { method: 'DELETE', path: '/company/' }
            ]
        },
        MANAGE_BLOCK: {
            code: 'manage_block',
            name: '管理统计标签',
            allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN],
            apis: [
                { method: 'GET', path: '/area' },
                { method: 'POST', path: '/area' },
                { method: 'PATCH', path: '/area' },
                { method: 'DELETE', path: '/area' },
                { method: 'PUT', path: '/price_rule' },
                { method: 'GET', path: '/energy_users' },
            ]
        },
        MANAGE_FORMULA: {
            code: 'manage_formula',
            name: '管理公式',
            allowedRoles: [ROLES.PLATFORM_ADMIN],
            apis: [
                { method: 'GET', path: '/formula' },
                { method: 'POST', path: '/formula' },
                { method: 'PATCH', path: '/formula' },
                { method: 'DELETE', path: '/formula' },
                { method: 'POST', path: '/formula/view' }
            ]
        },
        // 用户权限管理
        USER_MANAGEMENT: {
            code: 'manage_user',
            name: '管理用户',
            allowedRoles: [ROLES.PLATFORM_ADMIN, ROLES.COMPANY_ADMIN],
            apis: [
                { method: 'GET', path: '/user' },
                { method: 'PATCH', path: '/user' },
                { method: 'DELETE', path: '/user' },
                { method: 'POST', path: '/user' }
            ]
        },
        //数据维护
        DATA_MAINTENANCE: {
            code: 'data_maintenance',
            name: '数据维护',
            allowedRoles: [ROLES.PLATFORM_ADMIN],
            apis: [
                { method: 'GET', path: '/record' },
                { method: 'POST', path: '/record' },
                { method: 'PATCH', path: '/record' },
                { method: 'DELETE', path: '/record' },
            ],
        },
    },
}

// 基于allowedRoles自动生成角色权限配置
export const ROLE_PERMISSIONS = {
    [ROLES.PLATFORM_ADMIN]: getAllPlatformAdminPermissions(),
    [ROLES.COMPANY_ADMIN]: getPermissionsForRole(ROLES.COMPANY_ADMIN),
    [ROLES.NORMAL_USER]: getPermissionsForRole(ROLES.NORMAL_USER)
};

// 获取指定角色的所有权限
function getPermissionsForRole(role) {
    const permissions = [];

    // 遍历权限对象的所有类别
    Object.values(PERMISSIONS).forEach(category => {
        // 处理直接在类别下的权限
        Object.values(category).forEach(item => {
            if (item.code && item.allowedRoles && item.allowedRoles.includes(role)) {
                permissions.push(item.code);
            }
            // 处理嵌套的子类别
            else if (typeof item === 'object' && !item.code) {
                Object.values(item).forEach(subItem => {
                    if (subItem.code && subItem.allowedRoles && subItem.allowedRoles.includes(role)) {
                        permissions.push(subItem.code);
                    }
                });
            }
        });
    });

    return permissions;
}

// 平台管理员拥有所有权限
function getAllPlatformAdminPermissions() {
    const permissions = [];

    Object.values(PERMISSIONS).forEach(category => {
        Object.values(category).forEach(subcategory => {
            if (subcategory.code) {
                permissions.push(subcategory.code);
            } else if (typeof subcategory === 'object' && subcategory !== null) {
                Object.values(subcategory).forEach(item => {
                    if (item && item.code) {
                        permissions.push(item.code);
                    }
                });
            }
        });
    });

    return permissions;
}